<template>
  <div class="product-section">
    <h2 class="section-title">我们的代言</h2>
    <div class="product-list">
      <el-card
          v-for="(product, index) in visibleProducts"
          :key="product.id"
          shadow="hover"
          class="product-card"
          :ref="index === visibleProducts.length - 1 ? setLastItemRef : null"
      >
        <img :src="product.imageUrl" :alt="product.name" class="product-image" />
        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p>{{ truncateText(stripHtml(product.introduce), 30) }}</p>
          <el-button type="primary" plain @click="goToEndorsementDetail(product.id)">
            了解更多
          </el-button>
        </div>
      </el-card>
    </div>

    <!-- 加载中 -->
    <div v-if="loading" class="loading-text">加载中...</div>
    <!-- 空数据 -->
    <div v-if="!loading && products.length === 0" class="empty-text">暂无代言信息</div>

    <!-- 展开/收起按钮 -->
    <div v-if="!loading && products.length > initialCount" class="more-btn">
      <button class="elegant-btn" @click="toggleShowAll">
        {{ showAll ? '收起' : '展开全部' }}
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, nextTick } from 'vue'
import { endorsements } from '@/api/endorsements'
import { useRouter } from 'vue-router'
import { stripHtml, truncateText } from '@/utils/text-utils.js'

const router = useRouter()

const products = ref([])
const loading = ref(false)
const showAll = ref(false)
const initialCount = ref(8) // 默认 PC 端显示 8 个
const lastItemRef = ref(null)

// 设置最后一个元素的 ref
const setLastItemRef = (el) => {
  if (el) lastItemRef.value = el.$el || el
}

// 判断是否是手机端
const isMobile = () => window.innerWidth < 768

onMounted(async () => {
  initialCount.value = isMobile() ? 4 : 8
  loading.value = true
  try {
    const res = await endorsements()
    products.value = res.data.data || []
  } catch (error) {
    console.error('获取代言信息失败:', error)
  } finally {
    loading.value = false
  }
})

const visibleProducts = computed(() => {
  if (showAll.value) return products.value
  return products.value.slice(0, initialCount.value)
})

const goToEndorsementDetail = (id) => {
  router.push(`/endorsement/${id}`)
}

const toggleShowAll = async () => {
  if (showAll.value) {
    // 收起
    showAll.value = false
    await nextTick()
    // 滚动到最后一个代言
    if (lastItemRef.value) {
      lastItemRef.value.scrollIntoView({
        behavior: 'smooth',
        block: 'end'
      })
    }
  } else {
    // 展开
    showAll.value = true
  }
}
</script>

<style scoped>
.product-section {
  padding: 60px 0;
  background-color: #f9f9f9;
}

.section-title {
  text-align: center;
  font-size: 32px;
  margin-bottom: 40px;
  color: #333;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.product-card {
  transition: transform 0.3s;
}

.product-card:hover {
  transform: translateY(-5px);
}

.product-image {
  width: 100%;
  height: 160px;
  object-fit: contain;
  padding: 20px;
  margin-left: -20px;
}

.product-info {
  padding: 20px;
  text-align: center;
}

.product-info h3 {
  margin-bottom: 10px;
  font-size: 18px;
}

.product-info p {
  color: #666;
  margin-bottom: 15px;
  font-size: 14px;
  text-indent: 1em;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading-text,
.empty-text {
  text-align: center;
  color: #888;
  margin-top: 20px;
}

.more-btn {
  text-align: center;
  margin-top: 30px;
}

/* 高级简约按钮样式 */
.elegant-btn {
  background: transparent;
  border: 1px solid #dcdfe6;
  border-radius: 24px;
  padding: 10px 28px;
  font-size: 15px;
  color: #333;
  cursor: pointer;
  transition: all 0.3s ease;
  letter-spacing: 1px;
  font-weight: 500;
}

.elegant-btn:hover {
  border-color: #409EFF;
  color: #409EFF;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
  transform: translateY(-2px);
}

.elegant-btn:active {
  transform: translateY(0);
  box-shadow: 0 1px 4px rgba(64, 158, 255, 0.2);
}

/* 平板设备 (768px-1024px) */
@media (max-width: 1024px) {
  .product-list {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
  }

  .product-image {
    height: 140px;
  }
}

/* 移动设备 (<768px) */
@media (max-width: 767px) {
  .product-section {
    padding: 40px 0;
  }

  .section-title {
    font-size: 28px;
    margin-bottom: 30px;
  }

  .product-list {
    grid-template-columns: 1fr;
    gap: 15px;
    padding: 0 15px;
  }

  .product-card {
    display: flex;
    flex-direction: row;
    align-items: center;
  }

  .product-image {
    width: 120px;
    height: 120px;
    padding: 15px;
  }

  .product-info {
    padding: 15px;
    text-align: left;
  }
}

/* 小屏手机 (<480px) */
@media (max-width: 480px) {
  .product-card {
    flex-direction: column;
  }

  .product-image {
    width: 100%;
    height: 120px;
  }

  .product-info {
    text-align: center;
  }
}
</style>
